package com.yonyou.pmclouds.businessobject.util;

import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.billtype.entity.BillTypeVO;
import com.yonyou.pmclouds.businessobject.entity.BoAttrsVO;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.*;

import static com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts.*;

/**
 * 业务对象表结构读取
 *
 * @author zhangzhonghao
 */
public class BusinessObjectConfig {

    private ArrayList<BoAttrsVO> boAttrsVOS = new ArrayList<>();

    private String suffix = ".xml";

    public BusinessObjectConfig(String filePath, String fileName,String data_usage) throws BusinessException {
        String tempFilePath = filePath;
        if (!tempFilePath.endsWith("/") && !tempFilePath.endsWith("\\")) {
            tempFilePath += "/";
        }
        parse(tempFilePath + fileName + suffix,data_usage);
    }

    public BusinessObjectConfig(String filePath, List<String> fileNameList,String data_usage) throws BusinessException {
        String tempFilePath = filePath;
        if (!tempFilePath.endsWith("/") && !tempFilePath.endsWith("\\")) {
            tempFilePath += "/";
        }
        if(StringUtils.isEmpty(data_usage)){
            data_usage = BillTypeVO.DATA_USAGE_PRINT;
        }
        for (String fileName : fileNameList) {
            parse(tempFilePath + fileName + suffix,data_usage);
        }
    }

    private Document getDocument(String filePath) throws BusinessException {
        ClassLoader classLoader = this.getClass().getClassLoader();
        SAXReader saxReader = new SAXReader();
        try {
//            return saxReader.read(filePath);
            InputStream documentStream = classLoader.getResourceAsStream(filePath);
            if(documentStream == null){
                return null;
            }
            return saxReader.read(documentStream);
        } catch (DocumentException e) {
            throw new BusinessException("parse business object xml : " + e.getMessage());
        }
    }

    private void parse(String filePath,String data_usage) throws BusinessException {
        Document document = getDocument(filePath);
        if(document == null){
            return;
        }
        Element rootElement = document.getRootElement();
        Node tableNameNode = rootElement.selectSingleNode(TABLE_NAME);
        Node tableCodeNode = rootElement.selectSingleNode(TABLE_CODE);
        Element fieldsElement = (Element) rootElement.selectSingleNode(TABLE_FIELDS);
        for (Iterator<Element> iterator = fieldsElement.elementIterator(); iterator.hasNext(); ) {
            Element fieldElement = iterator.next();
            Node fieldFilterNode = fieldElement.selectSingleNode(FIELD_FILTER);
            if(fieldFilterNode != null && !data_usage.equals(fieldFilterNode.getStringValue()) ) {
                continue;
            }
            BoAttrsVO boAttrsVO = new BoAttrsVO();
            Node fieldCodeNode = fieldElement.selectSingleNode(FIELD_CODE);
            Node fieldNameNode = fieldElement.selectSingleNode(FIELD_NAME);
            Node fieldTypeNode = fieldElement.selectSingleNode(FIELD_TYPE);
            Node fieldPrimaryNode = fieldElement.selectSingleNode(FIELD_PRIMARY);
            Node fieldOptionsNode = fieldElement.selectSingleNode(FIELD_OPTIONS);
            boAttrsVO.setName(fieldNameNode.getStringValue());
            boAttrsVO.setCode(fieldCodeNode.getStringValue());
            boAttrsVO.setFieldtype(fieldTypeNode.getStringValue());
            boAttrsVO.setIsPrimary(fieldPrimaryNode.getStringValue());
            boAttrsVO.setBo_code(tableCodeNode.getStringValue());
            boAttrsVO.setBo_name(tableNameNode.getStringValue());
            // 云审批添加字段可选项
            if (fieldOptionsNode != null) {
                Element optionsElement = (Element) fieldOptionsNode;
                Map<String, String> optionMap = new HashMap<>();
                for (Iterator<Element> optionIterator = optionsElement.elementIterator(); optionIterator.hasNext(); ) {
                    Element optionElement = optionIterator.next();
                    optionMap.put(optionElement.selectSingleNode(OPTION_VALUE).getStringValue(),
                            optionElement.selectSingleNode(OPTION_NAME).getStringValue());
                }
                boAttrsVO.setFieldOptionMap(optionMap);
            }
            boAttrsVOS.add(boAttrsVO);
        }
    }

    public ArrayList<BoAttrsVO> getBoAttrsVOS() {
        return boAttrsVOS;
    }

}
